<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/5/9
 * Time: 11:05
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;

use think\Exception;

class Base
{
    protected $model = null;

    /**
     * @param array $where
     * @param string $field
     * @return mixed
     * @throws Exception
     */
    public function getBaseByList(array $where = [], string $field = '*')
    {
        try {
            $result = $this->model->getBaseByList($where, $field);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        return $result;
    }

    /**
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $field
     * @param string $orderBy
     * @return mixed
     * @throws Exception
     */
    public function getBaseByListPage(array $where = [], int $page = 1, int $limit = 10, string $field = '*', string $orderBy = 'id desc')
    {
        try {
            $row = $this->model->getBaseByListPage($where, $page, $limit, $field, $orderBy);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        return $row;
    }

    /**
     * 删除
     * @param int $id 删除的id
     * @param bool $delete 真删除还是加删除
     * @return bool
     * @throws Exception
     */
    public function getDeleteById(int $id = 0, bool $delete = false)
    {

        try {
            $row = $this->model->getBaseById($id);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        if (empty($row)) {
            throw new Exception("不存在");
        }
        try {
            $result = $this->model->destroy($row['id'], $delete);
        } catch (\Exception $e) {
            throw new Exception("删除失败");
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }



}